package com.ddd.demo.listviewdemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Created by doushuqi on 15/4/29.
 */
public class SimpleCursorAdapterFragment extends Fragment {
    private static final String TAG = "SimpleCursorAdapterFragment";
    private ListView mListView;
    private SimpleCursorAdapter mSimpleCursorAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        mListView = (ListView) view.findViewById(R.id.list_container);
        Cursor cursor = DataBaseHelper.getInstance(getActivity()).getReadableDatabase().query("person",
                new String[]{"_id", "name", "age", "info"}, null, null, null, null, null);
        mSimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item, cursor,
                new String[]{"name", "age", "info"},
                new int[]{R.id.title, R.id.number, R.id.iv}, 1);
        mListView.setAdapter(mSimpleCursorAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_add) {
            SQLiteDatabase database = DataBaseHelper.getInstance(getActivity()).getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "name");
            values.put("age", "age");
            values.put("info", R.drawable.ic_launcher);
            database.insert("person", null, values);
        } else if (id == R.id.action_clear) {
            SQLiteDatabase database = DataBaseHelper.getInstance(getActivity()).getWritableDatabase();
            database.execSQL("DELETE FROM person");
        }

        mSimpleCursorAdapter.changeCursor(DataBaseHelper.getInstance(getActivity()).getReadableDatabase().query("person",
                new String[]{"_id", "name", "age", "info"}, null, null, null, null, null));
        mSimpleCursorAdapter.notifyDataSetChanged();
        return true;
    }


}
